<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class ActiveController extends AdminController
{

    /**
     * 行为日志列表
     * @author huajie <banhuajie@163.com>
     */
    //砍价模块
    public function index()
    {
        $where = array();
        $title = $_GET["title"];//get上传到网页名称的值
        if ($title) {
            $where['title'] = $title;
        }
        $id = $_GET["id"];//get上传到网页ID的值
        if ($id) {
            $where['id'] = $id;
        }
        $list = $this->lists('Active', $where, "", true);//把Active的表放到网页上来

        int_to_string($list);
        $this->assign('_list', $list);
        $this->display();
    }

    //团购商品
    public function group()
    {
        $where = array();
        $title = $_GET["title"];
        if ($title) {
            $where['title'] = $title;
        }
        $where['type'] ='1';
        $list = $this->lists('Active_group', $where, "", true);

        foreach($list as $key=>$value){
            if($value['type'] == '1'){
                $list[$key]['type'] = '普通';
            }else{
                $list[$key]['type'] = '抽奖';
            }
        }

        int_to_string($list);
        $this->assign('_list', $list);


        $groupinfo = M("Active_group")->where($where)->find();
        $this->assign("groupinfo", $groupinfo);
        $this->display();
    }
//超级团购
    public function supergroup()
    {
        $where = array();
        $title = $_GET["title"];
        if ($title) {
            $where['title'] = $title;
        }
        $where['type'] ='2';
        $list = $this->lists('Active_group', $where, "", true);

        foreach($list as $key=>$value){
            if($value['type'] == '1'){
                $list[$key]['type'] = '普通';
            }else{
                $list[$key]['type'] = '抽奖';
            }
        }
        int_to_string($list);
        $this->assign('_list', $list);

        $this->display();
    }

//关联商品
    public function relation()
    {
        $where = array();
        $relation = $_GET["relation"];//get relation这个数据提交到网页上去
        if ($relation) {
            $where['relation'] = $relation;
            $this->assign("relation",$relation);
        }
        $goods_id = $_GET["goods_id"];
        if ($goods_id) {
            $where['id'] = $goods_id;
        }
        $title = $_GET["title"];
        if ($title) {
            $where['title'] = $title;
        }
        $groupid = $_GET['id'];
        $groupgoodsed = M("ActiveGoods")->where(array("active_id"=>$groupid))->field("goods_id")->select();
        if(count($groupgoodsed)){
            $grouprelation = array();
            $grouprelationgoods = array();
            foreach($groupgoodsed as $key=>$val){
                $grouprelation[$val['goods_id']] = '1';
                $grouprelationgoods[] = $val['goods_id'];
            }
            if ($relation) {
                $where['id'] = array("in",$grouprelationgoods);
                if ($goods_id && in_array($goods_id,$grouprelationgoods)) {
                    $where['id'] = $goods_id;
                }else{
                    $where['id'] = "";
                }
            }
        }
        $list = $this->lists('Good', $where, "", true);//把Good表显示在网页上
        int_to_string($list);

        if(count($groupgoodsed)){
            foreach($list as $key=>$val){
                if($grouprelation[$val['id']]>0){
                    $list[$key]['selectedgoods'] = '1';
                }
            }
        }
        $this->assign('_list', $list);
        $this->display();
    }
    public function setactiverelation(){
        $ids = $_POST['id'];
        $groupid = $_GET['id'];

        //获取已经存在的团购商品
        $groupgoodsed = M("ActiveGoods")->where(array("active_id"=>$groupid))->field("goods_id")->select();
        if(count($groupgoodsed)) {
            $grouprelation = array();
            foreach ($groupgoodsed as $key => $val) {
                $grouprelation[] = $val['goods_id'];
            }
            $diffgoods = array_diff($grouprelation,$ids);
        }
        foreach($ids as $key=>$val){
            $data = array();
            $data['goods_id'] = $val;
            $data['active_id'] = $groupid;
            M("ActiveGoods")->add($data);
        }
    }
//关联商品
    public function grouprelation()
    {
        $where = array();
        $relation = $_GET["relation"];//get relation这个数据提交到网页上去
        if ($relation) {
            $where['relation'] = $relation;
            $this->assign("relation",$relation);
        }
        $goods_id = $_GET["goods_id"];
        if ($goods_id) {
            $where['id'] = $goods_id;
        }
        $title = $_GET["title"];
        if ($title) {
            $where['title'] = $title;
        }
        $groupid = $_GET['id'];
        $groupgoodsed = M("GroupGoods")->where(array("group_id"=>$groupid))->field("goods_id")->select();
        if(count($groupgoodsed)){
            $grouprelation = array();
            $grouprelationgoods = array();
            foreach($groupgoodsed as $key=>$val){
                $grouprelation[$val['goods_id']] = '1';
                $grouprelationgoods[] = $val['goods_id'];
            }
            if ($relation) {
                $where['id'] = array("in",$grouprelationgoods);
                if ($goods_id && in_array($goods_id,$grouprelationgoods)) {
                    $where['id'] = $goods_id;
                }else{
                    $where['id'] = "";
                }
            }
        }
        $list = $this->lists('Good', $where, "", true);//把Good表显示在网页上
        int_to_string($list);

        if(count($groupgoodsed)){
            foreach($list as $key=>$val){
                if($grouprelation[$val['id']]>0){
                    $list[$key]['selectedgoods'] = '1';
                }
            }
        }
        $this->assign('_list', $list);
        $this->display();
    }
    public function setgrouprelation(){
        $ids = $_POST['id'];
        $groupid = $_GET['id'];

        //获取已经存在的团购商品
        $groupgoodsed = M("GroupGoods")->where(array("group_id"=>$groupid))->field("goods_id")->select();
        if(count($groupgoodsed)) {
            $grouprelation = array();
            foreach ($groupgoodsed as $key => $val) {
                $grouprelation[] = $val['goods_id'];
            }
            $diffgoods = array_diff($grouprelation,$ids);
        }
        foreach($ids as $key=>$val){
            $data = array();
            $data['goods_id'] = $val;
            $data['group_id'] = $groupid;
            M("GroupGoods")->add($data);
        }

    }
//*************************************************************************
    public function addActive()
    {
        //将xx页面提交的砍价的数据存到数据容器当中
        $data = array();
        $data['title'] = $_POST['title'];//名称
        $data['bargain_cycle'] = $_POST['bargain_cycle'];//
        $data['price'] = $_POST['price'];
        $data['bargain_price'] = $_POST['bargain_price'];
        $data['bargain_amount'] = $_POST['bargain_amount'];
        $data['aquota'] = $_POST['aquota'];
        $data['purchase'] = $_POST['purchase'];
        $data['sales_volume'] = $_POST['sales_volume'];
        $data['update_time'] = date("Y-m-d H:i:s");

        if ($_POST['id']) {
            //如果存在id 根据id更改砍价模块的数据
            M("Active")->where(array("id" => $_POST['id']))->save($data);//保存数据
        } else {
            //如果不存在id就直接新增数据
            $id = M("Active")->add($data);//新增
        }

        //我只要操作成功 就需要提示
        $this->success('操作成功', U('Active/index'));//跳转到这个网页
    }

    public function addGroup(){
        //
        $moit = array();
        $moit['title'] = $_POST['title'];
        $moit['type'] = $_POST['type'];
        $moit['cycle'] = $_POST['cycle'];
        $moit['clusters'] = $_POST['clusters'];
        $moit['lottery'] = $_POST['lottery'];
        $moit['relation'] = $_POST['relation'];
        $moit['stock'] = $_POST['stock'];
        $moit['privilege'] = $_POST['privilege'];
        $moit['lottery'] = $_POST['lottery'];
        $moit['sales_volume'] = $_POST['sales_volume'];
        $moit['update_time'] = $_POST['update_time'];
        $moit['denomination'] = $_POST['denomination'];
        $moit['conditions'] = $_POST['conditions'];
        $moit['application'] = $_POST['application'];
        $moit['validity'] = $_POST['validity'];

        if ($_POST['id']) {
            M("Active_group")->where(array("id" => $_POST['id']))->save($moit);//保存数据
        } else {
            $id = M("Active_group")->add($moit);//新增到
        }
        $this->success('操作成功', U('Active/group'));//跳转到这个网页
    }

    public function addSuperGroup(){
        $moit = array();
        $moit['title'] = $_POST['title'];
        $moit['type'] = $_POST['type'];
        $moit['cycle'] = $_POST['cycle'];
        $moit['clusters'] = $_POST['clusters'];
        $moit['lottery'] = $_POST['lottery'];
        $moit['relation'] = $_POST['relation'];
        $moit['stock'] = $_POST['stock'];
        $moit['privilege'] = $_POST['privilege'];
        $moit['lottery'] = $_POST['lottery'];
        $moit['sales_volume'] = $_POST['sales_volume'];
        $moit['update_time'] = $_POST['update_time'];
        $moit['denomination'] = $_POST['denomination'];
        $moit['conditions'] = $_POST['conditions'];
        $moit['application'] = $_POST['application'];
        $moit['validity'] = $_POST['validity'];

        if ($_POST['id']) {
            M("Active_group")->where(array("id" => $_POST['id']))->save($moit);//保存数据
        } else {
            $id = M("Active_group")->add($moit);//新增到
        }

        $this->success('操作成功', U('Active/supergroup'));//跳转到这个网页
    }
}










